#include "lvgl/lvgl.h"
#include "lvgl/demos/lv_demos.h"
#include <unistd.h>
#include <pthread.h>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <arpa/inet.h>
#include <string.h>
// #include <cjson/cJSON.h>
#include "./cJSON.h"

#include "./ui/ui.h"

static const char *getenv_default(const char *name, const char *dflt)
{
    return getenv(name) ?: dflt;
}

#if LV_USE_LINUX_FBDEV
static void lv_linux_disp_init(void)
{
    const char *device = getenv_default("LV_LINUX_FBDEV_DEVICE", "/dev/fb0");
    lv_display_t *disp = lv_linux_fbdev_create();

    lv_linux_fbdev_set_file(disp, device);
}
#elif LV_USE_SDL
static void lv_linux_disp_init(void)
{
    const int width = atoi(getenv("LV_SDL_VIDEO_WIDTH") ?: "800");
    const int height = atoi(getenv("LV_SDL_VIDEO_HEIGHT") ?: "480");

    lv_sdl_window_create(width, height);
}
#else
// #error Unsupported configuration
#endif

void my_chinese_font(char *text)
{
    // 1.创建中文字体
    lv_font_t *font = lv_freetype_font_create("/home/gec/simkai.ttf", // 👍加载字库文件
                                              LV_FREETYPE_FONT_RENDER_MODE_BITMAP,
                                              24,
                                              LV_FREETYPE_FONT_STYLE_NORMAL);

    // 2.创建中文字体样式
    static lv_style_t style;
    lv_style_init(&style);                                 // 初始化样式
    lv_style_set_text_font(&style, font);                  // 设置中文字体样式
    lv_style_set_text_align(&style, LV_TEXT_ALIGN_CENTER); // 字体居中显示

    lv_obj_t *label = lv_label_create(lv_screen_active());
    lv_obj_add_style(label, &style, 0); // 👍给标签添加中文字体
    lv_label_set_text(label, text);
    lv_obj_center(label);
}
int start_img_opa = 50;
lv_obj_t *img_init;
pthread_mutex_t mutex;
void *dish_video(void* arg)
{
    for (int i=1;;i++)
    {
        //拼接字符串
        char str[50] = {0};
		int num = i % 5;
		if(num == 0)
			continue;
        sprintf(str, "A:/home/gec/menu/广告%d.png", num);
		//上锁
		pthread_mutex_lock(&mutex);
		lv_image_set_src(ui_Imageguanggao, str);
		//解锁
		pthread_mutex_unlock(&mutex);
        sleep(5);
    }
}
void start_img_timer(lv_timer_t *timer)
{
    int step = 5;
    start_img_opa += step;
    if (start_img_opa >= 255)
    {
        start_img_opa = 255;
        lv_timer_del(timer);
        sleep(1);
        lv_obj_del(img_init);

        ui_init();
        pthread_t tid;
        pthread_create(&tid, NULL, dish_video, NULL);
        ui_init();  
    }
    else
    {
        lv_obj_set_style_img_opa(img_init, start_img_opa, 0);
    }
}

int main(void)
{
    
    lv_init();
    /*Linux display device init*/
    lv_linux_disp_init();
    
    create_font();

#if LV_USE_SDL
    // init input device
    lv_sdl_mouse_create();
    lv_sdl_keyboard_create();
    lv_sdl_mousewheel_create();
#endif

#if LV_USE_LINUX_FBDEV
    // 创建输入设备
    lv_indev_t *touch = lv_evdev_create(LV_INDEV_TYPE_POINTER, "/dev/input/event0");
    // 校准输入设备屏幕坐标
    // lv_evdev_set_calibration(touch, 0, 0, 1024, 600); // 黑色边框的屏幕
    lv_evdev_set_calibration(touch, 0, 0, 800, 480);  // 蓝色边框的屏幕
#endif
    system_init();  
    //创建一个图像
    img_init = lv_img_create(lv_scr_act());
    lv_img_set_src(img_init, "A:/home/gec/menu/江西小炒.png");
    lv_obj_center(img_init);
    //设置图像透明度一开始为50%
    lv_obj_set_style_img_opa(img_init, start_img_opa, 0);
    //定义一个定时器然后创建
    lv_timer_t *start_img = lv_timer_create(start_img_timer, 30, NULL);

    /*Handle LVGL tasks*/
    while (1)
    {
        //上锁
        pthread_mutex_lock(&mutex);
        lv_timer_handler();
        //解锁
        pthread_mutex_unlock(&mutex);
        usleep(5000);
    }
    return 0;
}
